import 'dart:typed_data';

import 'package:flutter/services.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/common/login/entity/base_info.dart';
import 'package:flutter_shop/pages/common/login/entity/user_info.dart';
import 'package:flutter_shop/pages/member/person_info/person_info_contract.dart';
import 'package:flutter_shop/pages/member/person_info/person_info_model.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/routers/navigator_utils.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:multi_image_picker/multi_image_picker.dart';

/// Title:PersonInfoPresenter <br/>
/// Description: <br/>
/// author:xiemiao <br/>
/// date: 2020-03-16  <br/>
/// version 1.0 <br/>
class PersonInfoPresenter extends BasePresenter<PersonInfoView> {
  PersonInfoModel mModel = PersonInfoModel();
  UserInfo mForm = UserInfo();

  ///图片
  Uint8List imgBytes;

  @override
  void initState() {
    super.initState();
    //初始化表单
    UserInfo u = BaseInfo.getUserInfo();
    mForm.fromJson(u.toJson());
  }

  ///保存个人信息
  void savePersonInfo() {
    if (Utils.strIsEmpty(mForm.realname)) {
      mView.showToast("请输入姓名");
      return;
    }
    if (Utils.strIsEmpty(mForm.phone)) {
      mView.showToast("请输入手机");
      return;
    }
    if (Utils.strIsEmpty(mForm.address)) {
      mView.showToast("请输入地址");
      return;
    }

    BaseInfo.setUserInfo(mContext, UserInfo().fromJson(mForm.toJson()));
    NavigatorUtils.goBack(mContext);
  }

  ///选择照片
  void chooseImage() async {
    try {
      int max = 1;
      List<Asset> resultList = await MultiImagePicker.pickImages(
        maxImages: max,
        enableCamera: true,
        materialOptions: MaterialOptions(
          statusBarColor: Utils.getColorText(CommonColors.primary),
          actionBarColor: Utils.getColorText(CommonColors.primary),
          actionBarTitle: "选择照片",
          allViewTitle: "所有照片",
          selectionLimitReachedText: "最多选择$max张",
          textOnNothingSelected: "请选择图片",
          useDetailsView: true,
          selectCircleStrokeColor: Utils.getColorText(CommonColors.white),
        ),
      );

      if (resultList != null && resultList.isNotEmpty) {
        ByteData byteData = await resultList[0].getByteData(quality: 50);
        imgBytes = byteData.buffer.asUint8List();
        updateStateWidget();
      }
    } catch (e) {
      print(e);
    }
  }
}
